package m2;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

public class Producer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1. 连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        //2. 创建队列
        c.queueDeclare("cookie", true, false, false, null);

        //3. 发送消息
        while (true) {

            System.out.println("输入消息：");
            String s = new Scanner(System.in).nextLine();

            // null改为MessageProperties.PERSISTENT_TEXT_PLAIN，消息改为持久化设置
            c.basicPublish("", "cookie", MessageProperties.PERSISTENT_TEXT_PLAIN, s.getBytes());

        }

    }

}
